package com.ljn.data.utils;


import javax.servlet.http.HttpServletRequest;
import org.springframework.util.StringUtils;

public class ServletUtils {
    public ServletUtils() {
    }

    public static String getParameter(HttpServletRequest request, String name) {
        return getParameter(request, name, "");
    }

    public static String getParameter(HttpServletRequest request, String name, String defaultValue) {
        String value = request.getParameter(name);
        return !StringUtils.isEmpty(value) ? value : defaultValue;
    }

    public static int getIntParameter(HttpServletRequest request, String name) {
        return getIntParameter(request, name, 0);
    }

    public static int getIntParameter(HttpServletRequest request, String name, int defaultValue) {
        String value = request.getParameter(name);
        return !StringUtils.isEmpty(value) ? Integer.parseInt(value) : defaultValue;
    }

    public static long getLongParameter(HttpServletRequest request, String name) {
        return getLongParameter(request, name, 0L);
    }

    public static long getLongParameter(HttpServletRequest request, String name, long defaultValue) {
        String value = request.getParameter(name);
        return !StringUtils.isEmpty(value) ? Long.parseLong(value) : defaultValue;
    }

    public static float getFloatParameter(HttpServletRequest request, String name) {
        return getFloatParameter(request, name, 0.0F);
    }

    public static float getFloatParameter(HttpServletRequest request, String name, float defaultValue) {
        String value = request.getParameter(name);
        return !StringUtils.isEmpty(value) ? Float.parseFloat(value) : defaultValue;
    }

    public static double getDoubleParameter(HttpServletRequest request, String name) {
        return getDoubleParameter(request, name, 0.0D);
    }

    public static double getDoubleParameter(HttpServletRequest request, String name, double defaultValue) {
        String value = request.getParameter(name);
        return !StringUtils.isEmpty(value) ? Double.parseDouble(value) : defaultValue;
    }

    public static boolean getBooleanParameter(HttpServletRequest request, String name) {
        return getBooleanParameter(request, name, false);
    }

    public static boolean getBooleanParameter(HttpServletRequest request, String name, boolean defaultValue) {
        String value = request.getParameter(name);
        if (StringUtils.isEmpty(value)) {
            return defaultValue;
        } else {
            return value.equals("true");
        }
    }
}

